package com.webserver.core;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * WebServer主类
 * WebServer是一个web容器,主要有两个作用:
 * 1.管理部署所有的web application
 * 2.与客户端浏览器建立TCP连接,并基于HTTP协议进行通信
 */
public class WebServer {
    //1.建立TCP连接,定义属性ServerSocket
    private ServerSocket serverSocket;

    //2.通过构造器进行初始化的操作[针对serverSocket的]
    public WebServer() {
        try {
            System.out.println("正在启动服务端...");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端启动完毕!!!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //3.start
    public void start(){
        //1.等待客户端的连接
        try {
            System.out.println("等待客户端连接!!!");
            Socket socket = serverSocket.accept();
            System.out.println("一个客户端连接了!!!");
            Thread server = new Thread(new ClientHandler(socket));
            server.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //4.main方法启动web容器
    public static void main(String[] args) {
        WebServer server = new WebServer();
        server.start();
    }


}
